<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2016/3/8
 * Time: 11:10
 */
namespace Admin\Controller;

use Admin\Builder\AdminConfigBuilder;
use Admin\Builder\AdminListBuilder;

class ChealthController extends AdminController{

    /**
     * 查体时间管理
     */
    public function manager(){
        $aPage = I('page', 1, 'intval');
        $r = 20;
        $model = M('ChealthTime');

        $list = $model->order('id desc')->page($aPage, $r)->select();
        unset($li);
        $totalCount = $model->count();

        //显示页面
        $builder = new AdminListBuilder();
        $attr1['class'] = 'btn btn-ajax btn-success';
        $attr1['href'] = $builder->addUrlParam(U('addManager'), array('type'=>1));

        $attr2 = $attr1;
        $attr2['href'] = $builder->addUrlParam(U('addManagerFu'), array('type'=>2));

        $attr_del['class'] = 'btn btn-danger ajax-post confirm';
        $attr_del['url'] = $builder->addUrlParam(U('delManager'));
        $attr_del['target-form'] = 'ids';

        $builder->title('查体时间管理')
            ->button('添加普通查体', $attr1)
            ->button('添加妇科查体', $attr2)
            ->button('删除', $attr_del)
            ->keyId()
            ->keyLink('name', '名称', 'enter?aid={$id}')
            ->key('type', '查体类型', 'status', array(1=>'普通查体',2=>'妇科查体'))
            ->key('reserve_start_time','预约开始时间', 'date')
            ->key('reserve_end_time','预约结束时间', 'date')
            ->key('check_start_time','查体开始时间', 'date')
            ->key('check_end_time','查体结束时间', 'date')
            ->keyDoActionEdit('editManager?id={$id}&type={$type}')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    /**
     * 添加查体时间
     */
    public function addManager(){
        if (IS_POST) {
            $model = D('Chealth/ChealthTime');
            $result = $model->updateChealthTime();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('添加成功', U('manager'));
        } else {
            $type = I('get.type', '1', 'intval');
            $time = strtotime(date('Y-m-d'));
            $data = array(
                'type'=>$type,
                'single_day_limit'=>30,
                'unit'=>'齐鲁工业大学',
                'reserve_start_time' => $time,
                'reserve_end_time' => $time,
                'check_start_time' => $time,
                'check_end_time' => $time,
            );
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加查体时间')
                ->keyText('name', '查体时间名称')
                ->keyTime('reserve_start_time', '预约开始时间', '', 'date')
                ->keyTime('reserve_end_time', '预约结束时间', '', 'date')
                ->keyTime('check_start_time', '查体开始时间', '', 'date')
                ->keyTime('check_end_time', '查体结束时间', '', 'date')
                ->key('check_exclude_time', '不可查体时间', '', 'timegroup')
                ->keyText('unit', '单位名称', '')
                ->keyInteger('single_day_limit', '每天报名人数限制', '0表示无限制')
                ->key('chealth_project', '查体项目', '费用为0代表免费', 'chealthproject')
                ->keyRadio('type', '类型', '', array('1'=>'普通查体','2'=>'妇科查体'))
                ->buttonSubmit(U('addManager'))
                ->data($data)
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 修改查体时间
     */
    public function editManager(){
        if(I('type')==2){
            redirect(U('editManagerFu', array('id'=>I('id'),'type'=>I('type'))));
        }
        if (IS_POST) {
            $model = D('Chealth/ChealthTime');
            $result = $model->updateChealthTime();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            S('enterList_'.$_POST['id'], NULL);
            $this->success('修改成功', U('manager'));
        } else {
            $id = I('id', 0, 'intval');
            $data = D('Chealth/ChealthTime')->getInfoById($id);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加查体时间')
                ->keyReadOnly('id', 'ID')
                ->keyText('name', '查体时间名称')
                ->keyTime('reserve_start_time', '预约开始时间', '', 'date')
                ->keyTime('reserve_end_time', '预约结束时间', '', 'date')
                ->keyTime('check_start_time', '查体开始时间', '', 'date')
                ->keyTime('check_end_time', '查体结束时间', '', 'date')
                ->key('check_exclude_time', '不可查体时间', '', 'timegroup')
                ->keyText('unit', '单位名称', '不填写则使用"齐鲁工业大学"')
                ->keyInteger('single_day_limit', '每天报名人数限制', '0表示无限制')
                ->key('chealth_project', '查体项目', '费用为0代表免费', 'chealthproject')
                ->keyRadio('type', '类型', '', array('1'=>'普通查体','2'=>'妇科查体'))
                ->buttonSubmit(U('editManager'))
                ->data($data)
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 添加妇科查体时间
     * 妇科查体为上午八点，下午两点
     */
    public function addManagerFu(){
        if (IS_POST) {
            $model = D('Chealth/ChealthTime');
            $result = $model->updateChealthTime();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('添加成功', U('manager'));
        } else {
            $type = I('get.type', '1', 'intval');
            $time = strtotime(date('Y-m-d'));
            $data = array(
                'type'=>$type,
                'single_day_limit'=>30,
                'unit'=>'齐鲁工业大学',
                'reserve_start_time' => $time,
                'reserve_end_time' => $time,
                'check_start_time' => $time,
                'check_end_time' => $time,
            );
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加查体时间')
                ->keyText('name', '查体时间名称')
                ->keyTime('reserve_start_time', '预约开始时间', '', 'date')
                ->keyTime('reserve_end_time', '预约结束时间', '', 'date')
                ->keyTime('check_start_time', '查体开始时间', '', 'datemf')
                ->keyTime('check_end_time', '查体结束时间', '', 'datemf')
                ->key('check_exclude_time', '不可查体时间', '', 'timegroupfu')
                ->keyText('unit', '单位名称', '')
                ->keyInteger('single_day_limit', '每天报名人数限制', '0表示无限制')
                ->key('chealth_project', '查体项目', '费用为0代表免费', 'chealthproject')
                ->keyRadio('type', '类型', '', array('1'=>'普通查体','2'=>'妇科查体'))
                ->buttonSubmit(U('addManagerFu'))
                ->data($data)
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 修改查体时间
     */
    public function editManagerFu(){
        if (IS_POST) {
            $model = D('Chealth/ChealthTime');
            $result = $model->updateChealthTime();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('修改成功', U('manager'));
        } else {
            $id = I('id', 0, 'intval');
            $data = D('Chealth/ChealthTime')->getInfoById($id);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加查体时间')
                ->keyReadOnly('id', 'ID')
                ->keyText('name', '查体时间名称')
                ->keyTime('reserve_start_time', '预约开始时间', '', 'date')
                ->keyTime('reserve_end_time', '预约结束时间', '', 'date')
                ->keyTime('check_start_time', '查体开始时间', '', 'datemf')
                ->keyTime('check_end_time', '查体结束时间', '', 'datemf')
                ->key('check_exclude_time', '不可查体时间', '', 'timegroupfu')
                ->keyText('unit', '单位名称', '不填写则使用"齐鲁工业大学"')
                ->keyInteger('single_day_limit', '每天报名人数限制', '0表示无限制')
                ->key('chealth_project', '查体项目', '费用为0代表免费', 'chealthproject')
                ->keyRadio('type', '类型', '', array('1'=>'普通查体','2'=>'妇科查体'))
                ->buttonSubmit(U('editManagerFu'))
                ->data($data)
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 删除查体时间
     */
    public function delManager(){
        $ids = I('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthTime');
        $res = $model->where(array('id'=>array('IN', $ids)))->delete();
        if($res > 0){
            $this->success('删除成功', U('manager'));
        }else{
            $this->error('删除失败,请重试');
        }
    }

    /**
     * 查体人员管理
     */
    public function enter()
    {
        $aid = I('aid', 0, 'intval');
        if (empty($aid)) {
            $this->error('参数错误', U('manager'));
        }
        $sign = I('sign', '');
        $name = I('name', '');
        $aPage = I('page', 1, 'intval');
        $r = 20;
        $model = D('Chealth/ChealthEnter');
        $map['aid'] = $aid;
        if (!empty($sign)) {
            $map['sign'] = array('like', "%$sign%");
        }
        if (!empty($name)) {
            $map['name'] = array('like', "%$name%");
        }
        $list = $model->where($map)->order('id asc')->page($aPage, $r)->select();
        unset($li);
        $totalCount = $model->where($map)->count();
        //获取预约报名项目信息
        $ainfo = D('Chealth/ChealthTime')->where(array('id' => $aid))->find();
        $aname = $ainfo['name'];
        if ($ainfo['type'] == 2){
            foreach ($list as &$v) {
                $hour = date('H', $v['chealth_time']);
                $v['chealth_time'] = date('Y-m-d', $v['chealth_time']);
                if ($hour == 0) {
                    $v['chealth_time'] .= ' 上午';
                } elseif ($hour == 12) {
                    $v['chealth_time'] .= ' 下午';
                }
            }
        }else{
            foreach ($list as &$v) {
                $v['chealth_time'] = date('Y-m-d', $v['chealth_time']);
            }
        }
        //显示页面
        $builder = new AdminListBuilder();
        $attr1['class'] = 'btn btn-ajax btn-success';
        $attr1['href'] = $builder->addUrlParam(U('addEnter?aid='.$aid));

        $attr_del['class'] = 'btn btn-danger ajax-post confirm';
        $attr_del['url'] = $builder->addUrlParam(U('delEnter'), array());
        $attr_del['target-form'] = 'ids';

        $attr2['class'] = 'btn btn-ajax btn-info';
        $attr2['href'] = $builder->addUrlParam(U('importChealthNum?aid='.$aid));

        $attr3['class'] = 'btn btn-ajax btn-info';
        $attr3['href'] = $builder->addUrlParam(U('exportEnter?aid='.$aid));

        $attr4['class'] = 'btn btn-ajax btn-info';
        $attr4['href'] = $builder->addUrlParam(U('importChealthReport?aid='.$aid.'&file_field=1'));

        $attr5['class'] = 'btn btn-ajax btn-info';
        $attr5['href'] = $builder->addUrlParam(U('importChealthReport?aid='.$aid.'&file_field=2'));

        $builder->title('['.$aname.']报名管理')
            ->button('添加', $attr1)
            ->button('删除', $attr_del)
            ->button('导出报名信息', $attr3)
            ->button('导入体检号', $attr2)
            ->button('导入普通体检报告', $attr4)
            ->button('导入妇科体检报告', $attr5)
            ->keyId()
            ->keyLink('name', '姓名', 'editEnter?id={$id}&aid={$aid}')
            ->keyText('sign', '工号')
            ->key('num', '体检号', '', 'text')
            ->key('chealth_time', '预约查体时间', 'text')
            ->keyDoActionEdit('editEnter?id={$id}&aid={$aid}')
            ->search('工号搜索', 'sign', 'text', '输入工号进行查询')
            ->search('姓名搜索', 'name', 'text', '输入工号进行查询')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    /**
     * 添加查体人员
     */
    public function addEnter(){
        $aid = I('aid', '', 'intval');
        if(empty($aid)){
            $this->error('参数错误!');
        }
        $this->assign('aid', $aid);
        $model = D('Chealth/ChealthEnter');
        //获取预约报名项目信息
        $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
        if(empty($ainfo)){
            $this->error('暂无预约项目');
        }
        if(IS_POST){
            //查找用户id
            $uid = D('UcenterMember')->where(array('username'=>I('sign')))->getField('id');
            if(empty($uid)){
                $this->error('系统中未找到该用户，请为其添加账号后再添加报名');
            }
            $_POST['uid'] = $uid;
            $result = $model->adminUpdateEnter();
            if (!$result) {
                $this->error($model->getError());
            }
            S('enterList_'.$ainfo['id'], NULL);
            //返回成功信息
            $this->success('添加预约成功', U('enter',array('aid'=>$aid)));
        }else{
            //获取部门信息
            $lists = D('ChealthDepartment')->select();
            $dmap = array();
            foreach ($lists as $key => $value) {
                $dmap[$value['id']] = $value['name'];
            }
            //获取分组列表
            $lists = D('ChealthGroups')->select();
            $gmap = array();
            foreach ($lists as $key => $value) {
                $gmap[$value['id']] = $value['name'];
            }
            //获取预约报名项目信息
            $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
            if(empty($ainfo)){
                $this->error('暂无预约项目', U('index'));
            }
            //处理查体项目
            if(!empty($ainfo['chealth_project'])){
                $len = count($ainfo['chealth_project']);
                $chealth_project = array();
                for($i = 0; $i < $len; $i += 2){
                    $chealth_project[$ainfo['chealth_project'][$i]] = $ainfo['chealth_project'][$i].'费用'.$ainfo['chealth_project'][$i+1].'元';
                }
            }
            $info = array(
                'aid' => $aid,
                'birthdate' => strtotime('-25 year'),
                'unit' => $ainfo['unit'],
                'update_num' => 0,
                'chealth_time' => strtotime(date('Y-m-d'))
            );
            if($ainfo['type'] == 2){
                $datetype = 'datemf';
            }else{
                $datetype = 'date';
            }
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加报名时间')
                ->keyText('sign', '标识号')
                ->keyText('name', '姓名')
                ->keyText('mobile', '手机号')
                ->keyText('card', '身份证号')
                ->keyTime('chealth_time', '查体时间', '', $datetype)
                ->keyCheckBox('project', '查体项目', '', $chealth_project)
                ->keySelect('sex', '性别', '', array('1'=>'男', '0'=>'女'))
                ->keyTime('birthdate', '出生日期', '', 'date')
                ->keyText('unit', '单位')
                ->keySelect('department_id', '部门', '', $dmap)
                ->keySelect('groups_id', '分组', '', $gmap)
                ->keySingleFile('file1', '普通体检报告')
                ->keySingleFile('file2', '妇科体检报告')
                ->keyText('update_num', '已修改次数')
                ->keySelect('status', '状态', '', C('FRONT_HEALTH_ENTER_STATUS'))
                ->data($info)
                ->buttonSubmit(U('addEnter'))
                ->buttonBack()
                ->keyHidden('aid','')
                ->display();
        }
    }

    /**
     * 修改查体人员
     */
    public function editEnter(){
        $id = I('id', '', 'intval');
        $aid = I('aid', '', 'intval');
        if(empty($aid)){
            $this->error('参数错误!');
        }
        $this->assign('aid', $aid);
        $model = D('Chealth/ChealthEnter');
        //检查是否已经报名
        $getinfo = $model->where(array('id'=>$id,'aid'=>$aid))->find();
        if(empty($getinfo)){
            $this->error('未报名!');
        }
        //获取预约报名项目信息
        $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
        if(empty($ainfo)){
            $this->error('暂无预约项目');
        }
        if(IS_POST){
            $result = $model->adminUpdateEnter();
            if (!$result) {
                $this->error($model->getError());
            }
            S('enterList_'.$ainfo['id'], NULL);
            //返回成功信息
            $this->success('修改预约成功', U('enter',array('aid'=>$aid)));
        }else{
            //获取部门信息
            $lists = D('ChealthDepartment')->select();
            $dmap = array();
            foreach ($lists as $key => $value) {
                $dmap[$value['id']] = $value['name'];
            }
            //获取分组列表
            $lists = D('ChealthGroups')->select();
            $gmap = array();
            foreach ($lists as $key => $value) {
                $gmap[$value['id']] = $value['name'];
            }
            //获取预约报名项目信息
            $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
            if(empty($ainfo)){
                $this->error('暂无预约项目', U('index'));
            }
            //处理查体项目
            if(!empty($ainfo['chealth_project'])){
                $len = count($ainfo['chealth_project']);
                $chealth_project = array();
                for($i = 0; $i < $len; $i += 2){
                    $chealth_project[$ainfo['chealth_project'][$i]] = $ainfo['chealth_project'][$i].'费用'.$ainfo['chealth_project'][$i+1].'元';
                }
            }
            if($ainfo['type'] == 2){
                $datetype = 'datemf';
            }else{
                $datetype = 'date';
            }
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加报名时间')
                ->keyReadOnly('num', '体检号')
                ->keyText('name', '姓名')
                ->keyText('mobile', '手机号')
                ->keyText('card', '身份证号')
                ->keyTime('chealth_time', '查体时间', '', $datetype)
                ->keyText('sign', '工号')
                ->keyCheckBox('project', '查体项目', '', $chealth_project)
                ->keySelect('sex', '性别', '', array('1'=>'男', '0'=>'女'))
                ->keyTime('birthdate', '出生日期', '', 'date')
                ->keyText('unit', '单位')
                ->keySelect('department_id', '部门', '', $dmap)
                ->keySelect('groups_id', '分组', '', $gmap)
                ->keySingleFile('file1', '普通体检报告')
                ->keySingleFile('file2', '妇科体检报告')
                ->keyText('update_num', '已修改次数')
                ->keySelect('status', '状态', '', C('FRONT_HEALTH_ENTER_STATUS'))
                ->data($getinfo)
                ->buttonSubmit(U('editEnter'))
                ->buttonBack()
                ->keyHidden('aid','')
                ->keyHidden('id','')
                ->display();
        }
    }

    /**
     * 删除查体人员
     */
    public function delEnter(){
        $ids = I('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthEnter');
        $res = $model->where(array('id'=>array('IN', $ids)))->delete();
        if($res > 0){
            $aid = D('Chealth/ChealthTime')->where(array('id'=>$ids[0]))->getField('aid');
            S('enterList_'.$aid, NULL);
            $this->success('删除成功');
        }else{
            $this->error('删除失败,请重试');
        }
    }

    /**
     * 导出报名人员
     * @return [type] [description]
     */
    public function exportEnter(){
        $aid = I('aid');
        if(empty($aid)){
            $this->error('参数错误!');
        }
        //获取查体时间的参数
        $alist = D('Chealth/ChealthTime')->getInfoById($aid);
        $alen = count($alist['chealth_project']);
        $project = array();
        for ($i=0; $i < $alen; $i+=2) { 
            $project[] = $alist['chealth_project'][$i];
        }
        //获取所有预约名单
        $list = D('Chealth/ChealthEnter')->where(array('aid'=>$aid))->select();
        //获取部门列表
        $lists = D('ChealthDepartment')->select();
        $dmap = array();
        foreach ($lists as $key => $value) {
            $dmap[$value['id']] = $value['name'];
        }
        //获取分组列表
        $lists = D('ChealthGroups')->select();
        $gmap = array();
        foreach ($lists as $key => $value) {
            $gmap[$value['id']] = $value['name'];
        }
        $i = 1;
        foreach ($list as &$v) {
            $v['i'] = $i;
            ++$i;
            $v['department'] = $dmap[$v['department_id']];
            $v['groups'] = $gmap[$v['groups_id']];
            $v['birthdate'] = date('Y/m/d', strtotime($v['birthdate']));
            if($alist['type'] == 2){
                $v['enter_time'] = date('Y/m/d', $v['chealth_time']);
                $hour = date('H', $v['chealth_time']);
                if($hour == 0){
                    $v['enter_time'] .= ' 上午';
                }elseif($hour == 12){
                    $v['enter_time'] .= ' 下午';
                }
            }else{
                $v['enter_time'] = date('Y/m/d', $v['chealth_time']);
            }
            $v['sex'] = $v['sex'] == 0 ? '女' : '男';
            if(!empty($v['project'])){
                $temp = explode(',', $v['project']);
                foreach ($temp as $value) {
                    $v[$value] = $value;
                }
            }
        }
        unset($v);
        $xlsName  = "预约名单导出";
        $xlsCell  = array(
            array('i', '序号'),
            array('card', '身份证号'),
            array('department','部门'),
            array('name','姓名'),
            array('sex','性别'),
            array('age','年龄'),
            array('sign','标识号'),
            array('unit','单位'),
            array('groups','分组'),
            array('enter_time','查体时间'),
            array('mobile','电话'),
            array('project','查体项目')
        );
        foreach ($project as $v) {
            $xlsCell[] = array($v, $v);
        }
        exportExcel($xlsName,$xlsCell,$list);

    }

    /**
     * 导入体检号
     */
    public function importChealthNum(){
        $aid = I('aid');
        if(empty($aid)){
            $this->error('参数错误!');
        }
        if(IS_POST){
            $fileid = I('xls');
            if(empty($fileid)){
                $this->error('先上传文件!');
            }
            $info = D('file')->find($fileid);
            $res = importExecl(THINK_PATH . '..' . $info['savepath'] . $info['savename']);
            if($res['error'] == 0){
                $this->error($res['data']);
            }
            if(empty($res['data'])){
                $this->error('请将表格另存为xls格式');
            }
            if($res['error'] != 1){
                $this->error($res['message']);
            }
            $list = $res['data'][0]['Content'];
            unset($list[1]);
            $num_col = 0;
            $sign_col = 0;
            foreach ($list[2] as $k => $v) {
                if($v == '体检号'){
                    $num_col = $k;
                }
                if($v == '标识号'){
                    $sign_col = $k;
                }
            }
            unset($list[2]);
            //查找数据库,进行体检号关联
            $model = D('Chealth/ChealthEnter');
            $model->startTrans();
            foreach($list as $k => $v){
                if(!empty($v[$num_col]) && !empty($v[$sign_col])){
                    $map = array(
                        'aid'=>$aid,
                        'sign'=>$v[$sign_col]
                    );
                    $data = array(
                        'status' => 3,
                        'num'=>$v[$num_col]
                    );
                    $model->where($map)->save($data);
                }
            }
            if($model->commit()){
                $this->success('导入关联数据成功', U('enter',array('aid'=>$aid)));
            }else{
                $model->rollback();
                $this->error('数据库写入失败,请重试');
            }

        }else{
            $info = D('Chealth/ChealthTime')->find($aid);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('[' . $info['name'] . ']关联数据导入')
                ->keySingleFile('xls', 'xls数据')
                ->buttonSubmit(U('importChealthNum'))
                ->buttonBack()
                ->keyHidden('aid', '')
                ->data(array('aid'=>$aid))
                ->display();
        }
    }

    /**
     * 导入体检报告
     */
    public function importChealthReport(){
        $aid = I('aid');
        if(empty($aid)){
            $this->error('参数错误!');
        }
        $file_field = I('file_field', 1);
        if(IS_POST){
            $fid = I('fid');
            if(empty($fid)){
                $this->error('先上传文件');
            }
            $model = D('Chealth/ChealthEnter');
            $model->startTrans();
            $list = D('file')->field('id,name')->where(array('id'=>array('in', $fid)))->select();
            foreach($list as $v){
                $id_arr = explode('_', $v['name']);
                if(count($id_arr) == 1){
                    $id_arr = explode('-', $v['name']);
                }
                $map = array(
                    'aid' => $aid,
                    'num' => $id_arr[0]
                );
                $data = array(
                    'file'.$file_field => $v['id'],
                    'status' => 4
                );
                $model->where($map)->save($data);
            }
            if($model->commit()){
                $this->success('导入体检报告成功');
            }else{
                $model->rollback();
                $this->error('数据库写入失败,请重试');
            }

        }else{
            $info = D('Chealth/ChealthTime')->find($aid);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('[' . $info['name'] . ']体检报告导入')
                ->keyMultiFile('fid', 'pdf文件')
                ->buttonSubmit(U('importChealthReport'))
                ->keyHidden('file_field', '')
                ->buttonBack()
                ->keyHidden('aid', '')
                ->data(array('aid'=>$aid,'file_field'=>$file_field))
                ->display();
        }
    }

    /**
     * 部门列表
     */
    public function department(){
        $aPage = I('page', 1, 'intval');
        $r = 20;
        $model = M('ChealthDepartment');

        $list = $model->order('id asc')->page($aPage, $r)->select();
        unset($li);
        $totalCount = $model->count();

        //显示页面
        $builder = new AdminListBuilder();
        $attr1['class'] = 'btn btn-ajax btn-success';
        $attr1['href'] = $builder->addUrlParam(U('addDepartment'));

        $attr_del['class'] = 'btn btn-danger ajax-post confirm';
        $attr_del['url'] = $builder->addUrlParam(U('delDepartment'), array());
        $attr_del['target-form'] = 'ids';

        $builder->title('部门管理')
            ->button('添加', $attr1)
            ->button('删除', $attr_del)
            ->keyId()
            ->keyText('name', '部门名称')
            ->keyDoActionEdit('editDepartment?id={$id}')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    /**
     * 添加部门
     */
    public function addDepartment(){
        if (IS_POST) {
            $model = D('Chealth/ChealthDepartment');
            $result = $model->editDepartment();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('添加成功', U('department'));
        } else {

            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加部门')
                ->keyText('name', '部门名称')
                ->buttonSubmit(U('addDepartment'))
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 修改部门
     */
    public function editDepartment(){
        $id = I('id', '', 'intval');
        if(empty($id)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthDepartment');
        if (IS_POST) {
            $result = $model->editDepartment();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('修改成功', U('department'));
        } else {
            $info = $model->find($id);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('编辑部门名称')
                ->keyId()
                ->keyText('name', '部门名称')
                ->data($info)
                ->buttonSubmit(U('editDepartment'))
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 删除部门
     */
    public function delDepartment(){
        $ids = I('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthDepartment');
        $res = $model->where(array('id'=>array('IN', $ids)))->delete();
        if($res > 0){
            $this->success('删除成功', U('department'));
        }else{
            $this->error('删除失败,请重试');
        }
    }

    /**
     * 分组列表
     */
    public function groups(){
        $aPage = I('page', 1, 'intval');
        $r = 20;
        $model = M('ChealthGroups');

        $list = $model->order('id asc')->page($aPage, $r)->select();
        unset($li);
        $totalCount = $model->count();

        //显示页面
        $builder = new AdminListBuilder();
        $attr1['class'] = 'btn btn-ajax btn-success';
        $attr1['href'] = $builder->addUrlParam(U('addGroups'));

        $attr_del['class'] = 'btn btn-danger ajax-post confirm';
        $attr_del['url'] = $builder->addUrlParam(U('delGroups'), array());
        $attr_del['target-form'] = 'ids';

        $builder->title('分组管理')
            ->button('添加', $attr1)
            ->button('删除', $attr_del)
            ->keyId()
            ->keyText('name', '分组名称')
            ->keyDoActionEdit('editGroups?id={$id}')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    /**
     * 添加分组
     */
    public function addGroups(){
        if (IS_POST) {
            $model = D('Chealth/ChealthGroups');
            $result = $model->editGroups();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('添加成功', U('groups'));
        } else {

            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('添加分组')
                ->keyText('name', '分组名称')
                ->buttonSubmit(U('addGroups'))
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 修改分组
     */
    public function editGroups(){
        $id = I('id', '', 'intval');
        if(empty($id)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthGroups');
        if (IS_POST) {
            $result = $model->editGroups();
            if (!$result) {
                $this->error($model->getError());
            }
            //返回成功信息
            $this->success('修改成功', U('groups'));
        } else {
            $info = $model->find($id);
            //显示页面
            $builder = new AdminConfigBuilder();
            $builder->title('编辑分组名称')
                ->keyId()
                ->keyText('name', '分组名称')
                ->data($info)
                ->buttonSubmit(U('editGroups'))
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 删除分组
     */
    public function delGroups(){
        $ids = I('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthGroups');
        $res = $model->where(array('id'=>array('IN', $ids)))->delete();
        if($res > 0){
            $this->success('删除成功', U('groups'));
        }else{
            $this->error('删除失败,请重试');
        }
    }

    /**
     * 公告管理
     * 精简功能,有待完善
     */
    public function notices(){
        $model = D('Chealth/ChealthNotices');
        if(IS_POST){
            $model->updateNotice();
            $this->success('保存成功');
        }else{
            $info = $model->find(1);
            $builder = new AdminConfigBuilder();
            $builder->title('公告管理')
                ->keyId()
                ->keyText('title', '标题')
                ->keyEditor('content', '内容')
                ->keyStatus('status', '状态')
                ->data($info)
                ->buttonSubmit(U('notices'))
                ->buttonBack()
                ->display();
        }
    }

    /**
     * 留言管理
     */
    public function comment(){
        $aPage = I('page', 1, 'intval');
        $r = 20;
        $model = M('ChealthComment');

        $list = $model->order('id desc')->page($aPage, $r)->select();
        unset($li);
        $totalCount = $model->count();
        foreach($list as $k => $v){
            $list[$k]['nickname'] = get_nickname($v['uid']);
            $str = $v['content'];
            $str = preg_replace( "@<script(.*?)</script>@is", "", $str );
            $str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
            $str = preg_replace( "@<style(.*?)</style>@is", "", $str );
            $str = preg_replace( "@<img(.*?)>@is", "[图片]", $str );
            $str = preg_replace( "@<(.*?)>@is", "", $str );
            $list[$k]['content'] = mb_substr($str,0,12,'utf-8') . '...';
        }
        //显示页面
        $builder = new AdminListBuilder();

        $attr_del['class'] = 'btn btn-danger ajax-post confirm';
        $attr_del['url'] = $builder->addUrlParam(U('delComment'));
        $attr_del['target-form'] = 'ids';

        $builder->title('查体时间管理')
            ->button('删除', $attr_del)
            ->keyId()
            ->keyNickname('nickname', '姓名')
            ->keyHtml('content', '内容')
            ->keyCreateTime('create_time','留言时间')
            ->keyDoAction('detailComment?id={$id}', '查看详情')
            ->data($list)
            ->pagination($totalCount, $r)
            ->display();
    }

    public function detailComment(){
        $id = I('id', 0, 'intval');
        if(empty($id)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthComment');

        $info = $model->find($id);
        $info['nickname'] = get_nickname($info['uid']);
        $builder = new AdminConfigBuilder();
        $builder->title('公告管理')
            ->keyReadOnly('nickname', '姓名')
            ->keyEditor('content', '内容')
            ->data($info)
            ->buttonBack()
            ->display();
    }

    public function delComment(){
        $ids = I('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $model = D('Chealth/ChealthComment');
        $res = $model->where(array('id'=>array('IN', $ids)))->delete();
        if($res > 0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败,请重试');
        }
    }
}